//
//  ViewController.m
//  文字效果
//
//  Created by unisedu on 15/8/13.
//  Copyright (c) 2015年 unisedu. All rights reserved.
//

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
{
    CAGradientLayer * gradientLayer;
    UIView *  gradientView;
}
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *authorLabel;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UIImageView *coverImageView;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    gradientLayer = [CAGradientLayer layer];
    gradientView = [UIView new];
    [self styleText];
//    [self addFullOverlay];
//    [self addTintedOverlay];
//    [self addBackgroundColorToText];
//    [self addTintedBackgroundColorToText];
//    [self addBlurView];
    [self addGradientOverlay];
}

-(void)styleText{
    [self.titleLabel setFont:[UIFont fontWithName:@"Avenir-Black" size:28]];
    [self.authorLabel setFont:[UIFont fontWithName:@"Avenir-Book" size:14]];
    [self.dateLabel setFont:[UIFont fontWithName:@"Avenir-Book" size:14]];
}
-(void)addFullOverlay{
    
    UIView * overlayView = [UIView new];
    [overlayView setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];
    [self.view insertSubview:overlayView aboveSubview:self.coverImageView];
    [overlayView setTranslatesAutoresizingMaskIntoConstraints:NO];
    
    NSLayoutConstraint * top = [NSLayoutConstraint constraintWithItem:overlayView
                                                          attribute:NSLayoutAttributeTop
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeTop
                                                         multiplier:1
                                                           constant:0];
    NSLayoutConstraint * left = [NSLayoutConstraint constraintWithItem:overlayView
                                                            attribute:NSLayoutAttributeLeft
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:self.view
                                                            attribute:NSLayoutAttributeLeft
                                                           multiplier:1
                                                             constant:0];
    NSLayoutConstraint * right = [NSLayoutConstraint constraintWithItem:overlayView
                                                            attribute:NSLayoutAttributeRight
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:self.view
                                                            attribute:NSLayoutAttributeRight
                                                           multiplier:1
                                                             constant:0];
    NSLayoutConstraint * bottom = [NSLayoutConstraint constraintWithItem:overlayView
                                                            attribute:NSLayoutAttributeBottom
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:self.view
                                                            attribute:NSLayoutAttributeBottom
                                                           multiplier:1
                                                             constant:0];

    [self.view addConstraints:@[top,left,right,bottom]];
    
}

-(void)addTintedOverlay{
    UIView * overlayView = [UIView new];
    [overlayView setBackgroundColor:[UIColor colorWithRed:0.5 green:0.2 blue:0 alpha:0.5]];
    [self.view insertSubview:overlayView aboveSubview:self.coverImageView];
    [overlayView setTranslatesAutoresizingMaskIntoConstraints:NO];
    
    NSLayoutConstraint * top = [NSLayoutConstraint constraintWithItem:overlayView
                                                            attribute:NSLayoutAttributeTop
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:self.view
                                                            attribute:NSLayoutAttributeTop
                                                           multiplier:1
                                                             constant:0];
    NSLayoutConstraint * left = [NSLayoutConstraint constraintWithItem:overlayView
                                                             attribute:NSLayoutAttributeLeft
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:self.view
                                                             attribute:NSLayoutAttributeLeft
                                                            multiplier:1
                                                              constant:0];
    NSLayoutConstraint * right = [NSLayoutConstraint constraintWithItem:overlayView
                                                              attribute:NSLayoutAttributeRight
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:self.view
                                                              attribute:NSLayoutAttributeRight
                                                             multiplier:1
                                                               constant:0];
    NSLayoutConstraint * bottom = [NSLayoutConstraint constraintWithItem:overlayView
                                                               attribute:NSLayoutAttributeBottom
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:self.view
                                                               attribute:NSLayoutAttributeBottom
                                                              multiplier:1
                                                                constant:0];
    
    [self.view addConstraints:@[top,left,right,bottom]];
}

-(void)addBackgroundColorToText{
    NSMutableParagraphStyle * style = [[NSMutableParagraphStyle defaultParagraphStyle]mutableCopy];
    [style setFirstLineHeadIndent:10.0];
    [style setHeadIndent:10];
    [style setTailIndent:0];
    NSDictionary * attributes = @{NSParagraphStyleAttributeName:style};
    NSAttributedString * attributedTitleText = [[NSAttributedString alloc]initWithString:@"Supplier woes suggest Apple Watch sales aren't great" attributes:attributes];
    [self.titleLabel setAttributedText:attributedTitleText];
    UIColor *textbackgroundColor =[UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    [self.titleLabel setBackgroundColor:textbackgroundColor];
    [self.authorLabel setBackgroundColor:textbackgroundColor];
    [self.dateLabel setBackgroundColor:textbackgroundColor];
}

-(void)addTintedBackgroundColorToText{
    NSMutableParagraphStyle * style = [[NSMutableParagraphStyle defaultParagraphStyle]mutableCopy];
    [style setFirstLineHeadIndent:10.0];
    [style setHeadIndent:10];
    [style setTailIndent:0];
    NSDictionary * attributes = @{NSParagraphStyleAttributeName:style};
    NSAttributedString * attributedTitleText = [[NSAttributedString alloc]initWithString:@"Supplier woes suggest Apple Watch sales aren't great" attributes:attributes];
    [self.titleLabel setAttributedText:attributedTitleText];
    UIColor *textbackgroundColor =[UIColor colorWithRed:0.4 green:0 blue:0 alpha:0.6];
    [self.titleLabel setBackgroundColor:textbackgroundColor];
    [self.authorLabel setBackgroundColor:textbackgroundColor];
    [self.dateLabel setBackgroundColor:textbackgroundColor];

}


-(void)addBlurView{
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView * overlayView =[[UIVisualEffectView alloc]initWithEffect:effect];
    [self.view insertSubview:overlayView aboveSubview:self.coverImageView];
    
    [overlayView setTranslatesAutoresizingMaskIntoConstraints:NO];
    
    NSLayoutConstraint * top = [NSLayoutConstraint constraintWithItem:overlayView
                                                            attribute:NSLayoutAttributeTop
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:self.titleLabel
                                                            attribute:NSLayoutAttributeTop
                                                           multiplier:1
                                                             constant:-30];
    NSLayoutConstraint * left = [NSLayoutConstraint constraintWithItem:overlayView
                                                             attribute:NSLayoutAttributeLeft
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:self.view
                                                             attribute:NSLayoutAttributeLeft
                                                            multiplier:1
                                                              constant:0];
    NSLayoutConstraint * right = [NSLayoutConstraint constraintWithItem:overlayView
                                                              attribute:NSLayoutAttributeRight
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:self.view
                                                              attribute:NSLayoutAttributeRight
                                                             multiplier:1
                                                               constant:0];
    NSLayoutConstraint * bottom = [NSLayoutConstraint constraintWithItem:overlayView
                                                               attribute:NSLayoutAttributeBottom
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:self.view
                                                               attribute:NSLayoutAttributeBottom
                                                              multiplier:1
                                                                constant:0];
    
    [self.view addConstraints:@[top,left,right,bottom]];
}
-(void)addGradientOverlay{

    
    UIColor * opaqueBlackColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0];
    
    UIColor * transparentBlackColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.0];
    
    gradientLayer.colors = [NSArray arrayWithObjects:(__bridge id)transparentBlackColor.CGColor,(__bridge id)opaqueBlackColor.CGColor,nil];

    [gradientView.layer insertSublayer:gradientLayer atIndex:0];
//    [gradientView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [gradientView setFrame:CGRectMake(0, self.view.frame.size.height-150, self.view.frame.size.width, 150)];
    [gradientLayer setFrame:gradientView.bounds];
    [self.view insertSubview:gradientView aboveSubview:self.coverImageView];
    
//    NSLayoutConstraint * top = [NSLayoutConstraint constraintWithItem:gradientView
//                                                            attribute:NSLayoutAttributeTop
//                                                            relatedBy:NSLayoutRelationEqual
//                                                               toItem:self.titleLabel
//                                                            attribute:NSLayoutAttributeTop
//                                                           multiplier:1
//                                                             constant:0];
//    NSLayoutConstraint * left = [NSLayoutConstraint constraintWithItem:gradientView
//                                                             attribute:NSLayoutAttributeLeft
//                                                             relatedBy:NSLayoutRelationEqual
//                                                                toItem:self.view
//                                                             attribute:NSLayoutAttributeLeft
//                                                            multiplier:1
//                                                              constant:0];
//    NSLayoutConstraint * right = [NSLayoutConstraint constraintWithItem:gradientView
//                                                              attribute:NSLayoutAttributeRight
//                                                              relatedBy:NSLayoutRelationEqual
//                                                                 toItem:self.view
//                                                              attribute:NSLayoutAttributeRight
//                                                             multiplier:1
//                                                               constant:0];
//    NSLayoutConstraint * bottom = [NSLayoutConstraint constraintWithItem:gradientView
//                                                               attribute:NSLayoutAttributeBottom
//                                                               relatedBy:NSLayoutRelationEqual
//                                                                  toItem:self.view
//                                                               attribute:NSLayoutAttributeBottom
//                                                              multiplier:1
//                                                                constant:0];
//    
//    [self.view addConstraints:@[top,left,right,bottom]];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillLayoutSubviews{
    [super viewDidLayoutSubviews];
    [gradientView setFrame:CGRectMake(0, self.view.frame.size.height-150, self.view.frame.size.width, 150)];
    [gradientLayer setFrame:gradientView.bounds];
//     NSLog(@"gradientView.bounds.origin.x=%fgradientView.bounds.origin.y=%fgradientView.bounds.size.width=%fgradientView.bounds.size.height=%f",gradientView.bounds.origin.x,gradientView.bounds.origin.y,gradientView.bounds.size.width,gradientView.bounds.size.height);
//    NSLog(@"gradientLayer.bounds.origin.x=%fgradientLayer.bounds.origin.y=%fgradientLayer.bounds.size.width=%fgradientLayer.bounds.size.height=%f",gradientLayer.bounds.origin.x,gradientLayer.bounds.origin.y,gradientLayer.bounds.size.width,gradientLayer.bounds.size.height);
}
@end
